package com.gopay.bis.dao.tpas.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.bis.dao.tpas.TpasOrderDAO;
import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.bank.BankOrderStatus;
import com.gopay.common.constants.bank.tpas.TpasOrderStatus;
import com.gopay.common.domain.bank.tpas.TpasOrder;
import com.gopay.common.util.DateUtils;

@Repository("tpasOrderDAO")
public class TpasOrderDAOImpl extends BaseDAOImpl<TpasOrder, String> implements TpasOrderDAO {

    @Override
    public List<TpasOrder> getTpasOrdersByGopayOrderId(String orderId) {
        StringBuilder s = new StringBuilder("from TpasOrder t where ");
        s.append("t.orderId = :orderId");
        
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("orderId", orderId);
        return findByHql(s.toString(), param);
    }

    @Override
    public int updateBackSuccess(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update TpasOrder set orderStatus = '" + TpasOrderStatus.BANK_CALL_SUCCESS + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') " + ",");
        hql.append("tranFinishTime = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }

    @Override
    public int updateBackFail(String orderId) {
        String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update TpasOrder set orderStatus = '" + TpasOrderStatus.ORDER_FAIL + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') " + ",");
        hql.append("tranFinishTime = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
    }

	@Override
	public int saveCpDebitAmount(String orderId, String amount) {
		String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update TpasOrder set cpDebitAmount = '" + amount + "' ,");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where orderId = '" + orderId + "'");

        return this.bulkUpdate(hql.toString());
		
	}

	@Override
	public int updateInAcctFail(String outOrderId, String status) {
		String curDate = DateUtils.getCurrDate();

        StringBuffer hql = new StringBuffer();
        hql.append("update TpasOrder set orderStatus = '" + status + "',");
        hql.append("lastUpdTs = to_date('" + curDate + "','yyyy-mm-dd hh24:mi:ss') ");
        hql.append("where outOrderId = '" + outOrderId + "'");

        return this.bulkUpdate(hql.toString());
	}

}
